# = test.newuser.1.rb - New User Registration Test
# See LICENSE.txt for licensing details.
#
# == Description
#To test the functionality to verify new user registration.

require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'
    
	class TC_NewUserRegistration_test < Test::Unit::TestCase
    
    def startup
      $UserName = randomChar(7)
      $Password = randomChar(10)
      $Email = randomChar(5) + '@' + randomChar(3) + '.com'
      
      @@ie = Watir::IE.start($SITE_URL)
	  end
	
	  def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
	  end
    
    def test_01_verifyHomeText
      assert(ie.contains_text(/Community Server/))		
	  end
  
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToRegistrationPage
      ie.link(:text, 'Join').click
      assert(ie.contains_text("Sign in name"))      
    end
    
    def test_04_populateRegistrationForm      
      assert(ie.text_field(:id, /Username$/).exists?)
      ie.text_field(:id, /Username$/).set($UserName)
      assert(ie.text_field(:id, /Username$/).verify_contains($UserName))
      
      assert(ie.text_field(:id, /Password$/).exists?)
      ie.text_field(:id, /Password$/).set($Password)
      assert(ie.text_field(:id, /Password$/).verify_contains($Password))
      
      assert(ie.text_field(:id, /Password2$/).exists?)
      ie.text_field(:id, /Password2$/).set($Password)
      assert(ie.text_field(:id, /Password2$/).verify_contains($Password))
          
      assert(ie.text_field(:id, /Email$/).exists?)
      ie.text_field(:id, /Email$/).set($Email)
      assert(ie.text_field(:id, /Email$/).verify_contains($Email))
      
      assert(ie.text_field(:id, /Email2$/).exists?)
      ie.text_field(:id, /Email2$/).set($Email)
      assert(ie.text_field(:id, /Email2$/).verify_contains($Email))      
    end
	  
	  def test_05_submitRegistrationForm      
      assert(ie.link(:id, /CreateAccount$/).exists?)
      ie.link(:id, /CreateAccount$/).click
	  end

    def test_07_verifyLogoutLinkExists
      
      
      assert(ie.link(:text, 'Sign out').exists?)
      #assert(ie.contains_text("Signed in as"))
    end
    
    def test_08_navigateToLogout
      ie.link(:text, 'Sign out').click
      assert(ie.contains_text("Return home"))
    end
    
    def test_09_verifyLogoutPageLinks
      assert(ie.link(:url, $SITE_URL).exists?)
      assert(ie.link(:text, 'Sign in').exists?)
      assert(ie.link(:text, 'Join').exists?)
      assert(ie.link(:text, 'Help').exists?)
      assert(ie.link(:text, 'Home').exists?)
      assert(ie.link(:text, 'Blogs').exists?)
      assert(ie.link(:text, 'Forums').exists?)
      assert(ie.link(:text, 'Photos').exists?)
      assert(ie.link(:text, 'Downloads').exists?)
      assert(ie.link(:text, 'Sign in').exists?)
      assert(ie.link(:text, 'Return home').exists?)
      
      assert_equal $SITE_URL, ie.link(:url, $SITE_URL).href

      assert_equal $SITE_URL, ie.link(:text, 'Home').href
      assert_equal $SITE_URL + 'blogs/', ie.link(:text, 'Blogs').href
      assert_equal $SITE_URL + 'forums/', ie.link(:text, 'Forums').href
      assert_equal $SITE_URL + 'photos/', ie.link(:text, 'Photos').href
      assert_equal $SITE_URL + 'files/', ie.link(:text, 'Downloads').href
      assert_equal $SITE_URL, ie.link(:text, 'Return home').href
    end
    
    def test_10_navigateHome
      assert(ie.link(:text, 'Sign in').exists?)
      assert(ie.link(:text, 'Return home').exists?)
      assert_equal $SITE_URL, ie.link(:text, 'Return home').href
      
      ie.link(:text, 'Return home').click
      test_01_verifyHomeText
    end
    
    def test_11_navigateToLogin
      ie.link(:text, 'Sign in').click
      assert(ie.contains_text("Sign in name"))
    end

    def test_12_verifyLinksOnLoginPage
        assert(ie.link(:text, 'Sign in').exists?)
        assert(ie.link(:text, 'Join').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Home').exists?)
        assert(ie.link(:text, 'Blogs').exists?)
        assert(ie.link(:text, 'Forums').exists?)
        assert(ie.link(:text, 'Photos').exists?)
        assert(ie.link(:text, 'Downloads').exists?)
        assert(ie.link(:text, 'I forgot my password').exists?)

        assert_equal $SITE_URL, ie.link(:text, 'Home').href
        assert_equal $SITE_URL + 'blogs/', ie.link(:text, 'Blogs').href
        assert_equal $SITE_URL + 'forums/', ie.link(:text, 'Forums').href
        assert_equal $SITE_URL + 'photos/', ie.link(:text, 'Photos').href
        assert_equal $SITE_URL + 'files/', ie.link(:text, 'Downloads').href
        assert_equal $SITE_URL + 'user/EmailForgottenPassword.aspx', ie.link(:text, 'I forgot my password').href
      end
      
      def test_13_populateLoginForm
      ie.text_field(:id, /username$/).value = $UserName
      assert(ie.text_field(:id, /username$/).verify_contains($UserName))
      
      ie.text_field(:id, /password$/).value = $Password
      assert(ie.text_field(:id, /password$/).verify_contains($Password))
	  end
	  
    def test_14_submitLoginForm
      ie.link(:id, /loginButton$/).click
      test_01_verifyHomeText
    end
    
    def test_15_performLogout
      test_07_verifyLogoutLinkExists
      test_08_navigateToLogout
      test_09_verifyLogoutPageLinks
      test_10_navigateHome
    end
    
    def test_99_closeIE
      ie.close
    end
    
  end